Micron Document
Fox's Git Mirrors


Displaying Raw • Download

pkg/pageserver/dynamicpage/dynamic_test.go 83609bcb589eb3cfc35b98ecc0b3327b2ae99d77 (83609bcb) Text, 2.50 KB

// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024-2026 Quad4.io

package dynamicpage

import (
"bytes"
"os"
"path/filepath"
"runtime"
"testing"
)

func TestReadOrExecuteShebangExecutableRuns(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shebang execution is not supported the same way on windows")
}
dir := t.TempDir()
p := filepath.Join(dir, "prop_script.mu")
if err := os.WriteFile(p, []byte("#!/bin/sh\\necho 'script output'\\n"), 0o755); err != nil {
t.Fatal(err)
}
out, err := ReadOrExecute(p, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
if got := bytes.TrimSpace(out); !bytes.Equal(got, []byte("script output")) {
t.Fatalf("got %q want script output", got)
}
}

func TestReadOrExecuteShebangNotExecutableServesRaw(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "prop_script.mu")
if err := os.WriteFile(p, []byte("#!/bin/sh\\necho hi\\n"), 0o644); err != nil {
t.Fatal(err)
}
out, err := ReadOrExecute(p, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(out, []byte("#!/bin/sh")) {
t.Fatalf("expected raw file, got %q", out)
}
}

func TestReadOrExecuteExecutableNoShebangServesRaw(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "plain.mu")
if err := os.WriteFile(p, []byte("plain text\\n"), 0o755); err != nil {
t.Fatal(err)
}
out, err := ReadOrExecute(p, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(out, []byte("plain text\\n")) {
t.Fatalf("got %q", out)
}
}

func TestReadOrExecuteNonMuShebangExecutableIgnored(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shebang execution is not supported the same way on windows")
}
dir := t.TempDir()
p := filepath.Join(dir, "other.txt")
if err := os.WriteFile(p, []byte("#!/bin/sh\\necho x\\n"), 0o755); err != nil {
t.Fatal(err)
}
out, err := ReadOrExecute(p, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(out, []byte("#!/bin/sh")) {
t.Fatalf("expected raw file for non-.mu, got %q", out)
}
}

func BenchmarkReadOrExecuteStaticSmallMU(b *testing.B) {
dir := b.TempDir()
p := filepath.Join(dir, "page.mu")
if err := os.WriteFile(p, []byte(">Title\\n\\nbody\\n"), 0o644); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := ReadOrExecute(p, nil, nil, nil); err != nil {
b.Fatal(err)
}
}
}

func BenchmarkBuildScriptEnvEmptyData(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
_ = buildScriptEnv(nil, nil, nil)
}
}

Served by rngit 1.5.2 - Generated in 0.02s